package easyapache;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;

public class RequestHandler extends Thread{
	private static final String ROOT_PATH="D:\\projects\\chaojicode\\easyapache\\htdocs";
	private Socket socket;
	private InputStream in;
	private OutputStream out;
	
	
	public RequestHandler(Socket socket) {
		this.socket = socket;
		try {
			this.in = socket.getInputStream();
			this.out = socket.getOutputStream();
		} catch (IOException e) {
			System.out.println("获取流失败");
			e.printStackTrace();
		}
		
	}
	public String getFilePath() {
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf-8") );
			String httpMessage = reader.readLine();
			String[] content = httpMessage.split(" "); // GET /aaa.html HTTP/1.1
			if (content.length!=3) {
				return null;
			}
			return content[1];
		} catch (UnsupportedEncodingException e) {
			System.out.println("不支持的编码格式");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("IO异常");
			e.printStackTrace();
		}
		
		return null;
	}
	
	public void sendFileContent(String filePath) {
		try {
			File file = new File(RequestHandler.ROOT_PATH+filePath);
			System.out.println(file.getAbsolutePath());
			if (!file.exists()) {
				sendErrorMessage(404, "file not found");
			}
			BufferedInputStream bin = new BufferedInputStream(new FileInputStream(file));
			BufferedOutputStream bout = new BufferedOutputStream(out);
			byte[] bytes = new byte[2048];
			int n = -1;
			while ((n = bin.read(bytes,0,bytes.length)) != -1) {
	            //转换成字符串
	            String str = new String(bytes,0,n,"UTF-8");
	            //写入相关文件
	            bout.write(bytes, 0, n);
	        }
	        //清除缓存
	        bout.flush();
	        //关闭流
	        bout.close();
	        bin.close();
		} catch (FileNotFoundException e) {
			System.out.println("没有对应文件");
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			System.out.println("不支持的编码格式");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("IO异常");
			e.printStackTrace();
		}
		
		
	}
	
	public void sendErrorMessage(int errorCode,String errorMessage) {
		PrintWriter writer = new PrintWriter(out);
		writer.println("HTTP/1.0 " + errorCode + " " + errorMessage);
		writer.println("content-type: text/html");
		writer.println();
		writer.println("<html>");
		writer.println("<head>");
		writer.println("<title>Error Message");
		writer.println("</title>");
		writer.println("</head>");
		writer.println("<body>");
		try {
			writer.println("<h1>ErrorCode:" + errorCode + " ErrorMessage:" + new String(errorMessage.getBytes("utf-8")) + "</h1>");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		writer.println("</body>");
		writer.println("</html>");
		writer.flush();
		writer.close();
	}
	
	@Override
	public void run() {
		String filePath = getFilePath();
		System.out.println(filePath);
		sendFileContent(filePath);
	}

}
